##########################################
## "Voters get what they want"          ##
##########################################
## Heinrich, Kobayashi, Long            ##
##########################################

##          
## Substantive effects for subset models
## Script 04
## June 29, 2017


## Load estimated models
########################
to_get <- list.files(path="output/analysis1_subset/", pattern="Output", full.names=TRUE)
eff <- vector("list", length(to_get))
for(i in 1:length(to_get))
{
  load(to_get[i])
  eff[[i]] <- output$SubstEff
}
eff <- ldply(.data=eff, .fun=function(x) x)


## Print/save effects
#####################
eff_tmp <- cbind(eff[, 1:4], round(eff[, 5:ncol(eff)], di=2)) 
write.csv(eff_tmp, "output/tables/A1-Subset-Effects-Table.csv")


## Marginal effect | Hierarchy
##############################
eff$HierType <- factor(eff$HierType, levels=rev(c("Security hierarchy", "Economic hierarchy")))
for(i in 1:2)
{
  for(j in 1:4)
  {
    if(j == 1) eff2 <- subset(eff, NewsSet == "Lowest 40")
    if(j == 2) eff2 <- subset(eff, NewsSet == "Top 40")
    if(j == 3) eff2 <- subset(eff, NewsSet == "Lowest 20")
    if(j == 4) eff2 <- subset(eff, NewsSet == "Top 20")
    if(i == 1) eff2 <- subset(eff2, NewsType == "Leader")
    if(i == 2) eff2 <- subset(eff2, NewsType == "Human rights")
    g <- ggplot(data=eff2, aes(x=Hier, ymin=Q025, y=Q500, ymax=Q975))
    g <- g + geom_hline(yintercept=0, , size=1.2)
    g <- g + geom_ribbon(alpha=.7, colour=NA, fill="black")
    g <- g + geom_line(size=.9) 
    g <- g + scale_y_continuous(name="Change in total aid/ capita", labels=scales::percent)
    g <- g + facet_wrap( ~ HierType, scales="free_x") + theme_bw()
    g <- g + xlab("Hierarchy") + ggtitle(ifelse(j %in% c(1, 3), "Effect of human rights violations on foreign aid under low news coverage",
                                                "Effect of human rights violations on foreign aid under high news coverage"))
    g <- g + theme(axis.text = element_text(size=rel(.8)),
                   panel.grid.major.y = element_line(size=0.25, colour="grey80", 
                                                     linetype="dashed"),
                   panel.grid.major.x = element_blank(),               
                   panel.grid.minor = element_blank(),
                   axis.ticks = element_blank(),
                   strip.text = element_text(size=rel(1.2), hjust=0),
                   strip.background = element_blank(),
                   plot.title = element_text(size=rel(1.45), face="bold", hjust=0))
    ggsave(file=paste0("output/figures/A1-ME-Subset-", 
                       str_replace_all(string=eff2$NewsSet[1], pattern=" ", replacement=""), "-",
                       str_replace_all(string=eff2$NewsType[1], pattern=" ", replacement=""), ".pdf"),
           width=10.5, height=5.5)
  }
}


